package io.renren.modules.app.controller;

import io.jsonwebtoken.Claims;
import io.renren.common.dto.TeachHistory;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.service.ApiCollectService;
import io.renren.modules.app.service.ApiTeachService;
import io.renren.modules.app.utils.JwtUtils;
import io.renren.modules.sys.entity.HistoryEntity;
import io.renren.modules.sys.entity.SongEntity;
import io.renren.modules.sys.entity.TeachEntity;
import io.renren.modules.sys.entity.VedioEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 教程
 * @author wangyi
 * @mail 794651397@qq.com
 * @createdate 2018/3/16 15:13
 **/
@Api(description = "教程")
@RestController
@RequestMapping("/app")
public class ApiTeachController {
    @Autowired
    private ApiTeachService apiTeachService;
    @Autowired
    private ApiCollectService apiCollectService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private JwtUtils jwtUtils;
    
    /**
     * 获取教程 基础 1,进阶 2,考级 3
     * @author wangyi
     * @createdate 2018/3/16 15:14
     **/
    @ApiOperation("获取教程 基础 1,进阶 2,考级 3")
     @PostMapping("teachListByType")
     public R teachListByType(Integer teachType,String keywords){
        HashMap<String,Object> map=new HashMap<>();
        map.put("teachType",teachType);
        if(StringUtils.isNotBlank(keywords)){
            map.put("keywords",keywords);
        }
         List<TeachEntity> list =apiTeachService.teachListByType(map);

         return R.ok().put("result",list);
     }

     /**
      * 根据教程id获取详情
      * @author wangyi
      * @createdate 2018/3/19 11:27
      **/
     @ApiOperation("根据教程id获取详情 *{如果登陆,必须传token}*")
      @PostMapping("teachDetail")
      public R teachDetail(Long id){
          TeachEntity teachEntity=apiTeachService.queryTeachById(id);
          List<SongEntity> songList=apiTeachService.querySongListByTeach(id);
          List<VedioEntity> vedioList=apiTeachService.queryVedioListByTeach(id);

         HashMap<String,Object> result=new HashMap<>();
         result.put("collect",false);
         result.put("detail",teachEntity);
         result.put("songList",songList);
         result.put("vedioList",vedioList);

         //获取用户凭证
         String token = request.getHeader(jwtUtils.getHeader());
         if(StringUtils.isBlank(token)){
             token = request.getParameter(jwtUtils.getHeader());
         }
         //token不为空
         if(StringUtils.isNotBlank(token)){
             Claims claims = jwtUtils.getClaimByToken(token);
             if(claims != null && !jwtUtils.isTokenExpired(claims.getExpiration())){
                 Long userId=Long.parseLong(claims.getSubject());
                 HashMap<String,Object> map=new HashMap<>();
                 map.put("userId",userId);
                 map.put("teachId",id);
                 int count=apiCollectService.teachCollectCount(map);
                 if(count>0){
                     result.put("collect",true);
                 }
             }
         }
         return R.ok().put("result",result);

      }

    /**
     * 获取我浏览的教程
     *
     * @author wangyi
     * @createdate 2018/3/19 10:55
     **/
    @ApiOperation("获取我浏览的教程 *{需要登陆的token}*")
    @Login
    @PostMapping("getMyTeach")
    public R getMyTeach() {
        Long userId = (Long) request.getAttribute("userId");
        List<TeachHistory> teachEntityList = apiTeachService.getMyTeach(userId);
        return R.ok().put("result", teachEntityList);
    }

}
